package com.action;

import java.io.PrintWriter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.bean.Users;
import com.services.LoginServices;
import com.util.XDataConvert;

@Controller
@RequestMapping("/user")
public class LogicAction {
	@Resource(name = "loginService")
	private LoginServices services;

	@RequestMapping("/user/login.action")
	public void login(HttpServletRequest request, HttpServletResponse response,
			Users user) {
		if (user != null) {
			Users u = services.login(user);
			PrintWriter writer = null;
			try {
				writer = response.getWriter();
				if (u != null) {
					request.getSession().setAttribute("user", u);
					writer.print("1");
				} else {
					writer.print("2");
				}
			} catch (Exception e) {
				writer.print(e);
			} finally {
				if (null != writer) {
					writer.flush();
					writer.close();
				}
			}
		}
	}

	@RequestMapping("/user/chkPasscode.action")
	public void chkPasscode(HttpServletRequest request,
			HttpServletResponse response, String passcode) {
		PrintWriter writer = null;
		String SessionPasscode = XDataConvert.TryToString(request.getSession()
				.getAttribute("rand_yzm"));

		try {
			writer = response.getWriter();
			if (!passcode.equals(SessionPasscode)) {
				writer.print("1");
			} else {
				writer.print("2");
			}
		} catch (Exception e) {
			writer.print(e);
		} finally {
			if (null != writer) {
				writer.flush();
				writer.close();
			}
		}
	}

	@RequestMapping("/user/zhmm.action")
	public void zhmm(HttpServletRequest request, HttpServletResponse response,
			Users user) {
		if (user != null) {
			String msg = services.zhmm(user);
			PrintWriter writer = null;
			try {
				writer = response.getWriter();
				writer.print(msg);
			} catch (Exception e) {
				writer.print(e);
			} finally {
				if (null != writer) {
					writer.flush();
					writer.close();
				}
			}
		}
	}

	@RequestMapping(value = "/zcxyh")
	public String zcxyh(Users user, HttpServletRequest request, HttpServletResponse response) {
		// 获取保存的路径
		String realPath = request.getSession().getServletContext()
				.getRealPath("/")
				+ "/upload/userImg";
		String newFileName = services.scyhtx(realPath, user.getYhtx());
		String Imgurl = realPath + "/" + newFileName;
		user.setUrl(Imgurl);
		boolean flag = services.zcxyh(user);
		if (flag) {
			System.out.println(flag);
		}
		return "admin/login";
	}
}
